﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using CMSWCSFSolution.CMS.Interface.Services;
using CMS.Model;
using CMS.Utility;

namespace CMSWCSFSolution.CMS.Views
{
    public class CourseReportPresenter : Presenter<ICourseReportView>
    {
        private ICMSService _iCMSService;
        private int _rowCount;

        /// <summary>
        /// Create Service
        /// </summary>
        /// <param name="iCMSService"></param>
        public CourseReportPresenter([CreateNew] ICMSService iCMSService)
        {
            _iCMSService = iCMSService;
        }

        /// <summary>
        /// Loads the page
        /// </summary>
        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
        }

        /// <summary>
        /// Initializes the page
        /// </summary>
        public override void OnViewInitialized()
        {
            View.TeacherList = _iCMSService.GetAllTeacher();
            View.LevelList = _iCMSService.GetAllLevels();
        }

        /// <summary>
        /// Gets Courses by Condition
        /// </summary>
        /// <returns>List of CourseList</returns>
        public List<CourseList> GetCoursesByCondition()
        {
            Condition condition = new Condition();
            condition.CourseName = View.InputCourseName;
            condition.CourseStatus = View.InputCourseStatus;
            condition.TeacherName = View.InputTeacherName;
            condition.LevelName = View.InputLevelName;
            condition.OrderDirection = View.OrderDirection;
            condition.SortExpression = View.SortExpression;

            List<CourseList> courseList = new List<CourseList>();
            courseList=_iCMSService.GenerateCourseReport(condition,View.PageIndex,View.PageSize,out _rowCount);
            foreach (var course in courseList)
            {
                if (course.CourseStatus == true)
                {
                    course.StatusDisplay = "Open";
                }
                else
                {
                    course.StatusDisplay = "Closed";
                }
            }
            View.RowCount = _rowCount;

            var type = (CourseListComparer.CourseListcomparer.CompareType)Enum.Parse(typeof(CourseListComparer.CourseListcomparer.CompareType), View.SortExpression);
            CourseListComparer.CourseListcomparer.CourseListComparer courseListComparer = new CourseListComparer.CourseListcomparer.CourseListComparer(type);

            courseList.Sort(courseListComparer);
            if (View.OrderDirection == "desc")
            {
                courseList.Reverse();
            }
            return courseList;
        }
    }
}




